package org.one.stone.soup.grfxML.plugin.control;

import org.one.stone.soup.grfxML.DataKey;
import org.one.stone.soup.grfxML.DataState;
import org.one.stone.soup.grfxML.DataText;
import org.one.stone.soup.grfxML.GrfxMLEngine;
import org.one.stone.soup.grfxML.plugin.SimplePlugin;
import org.one.stone.soup.grfxML.plugin.grfxMLCaster;
import org.one.stone.soup.mjdb.data.field.Field;
import org.one.stone.soup.mjdb.data.field.LockException;
import org.one.stone.soup.stringhelper.StringArrayHelper;

public class KeyMapping extends SimplePlugin {
	private DataText map = new DataText();
	private DataKey keyboard;

	private DataState[] triggers;
	private boolean[] toggled;
	private char[] keys;
	private boolean[] ctrl;
	private boolean[] alt;
	private boolean[] fn;
	private boolean[] reset;
/**
 * FileToText constructor comment.
 */
public KeyMapping(GrfxMLEngine engine) {
	super(engine);
}
/**
 * initialize method comment.
 */
public void initialize()
{
	keyboard = getEngine().getAPI().getKey();

	triggers = new DataState[map.size()];
	reset = new boolean[map.size()];
	keys = new char[map.size()];
	fn = new boolean[map.size()];
	ctrl = new boolean[map.size()];
	alt = new boolean[map.size()];
	toggled = new boolean[map.size()];

	for(int loop=0;loop<map.size();loop++)
	{
		String m = map.get(loop).getValue();
		ctrl[loop]=false;
		alt[loop]=false;
		fn[loop]=false;
		toggled[loop]=false;

		String args[] = org.one.stone.soup.stringhelper.StringArrayHelper.parseFields(m,"=");
		for(int loop2=0;loop2<args.length;loop2++)
		{
			args[loop2].trim();
		}

		String parts[] = StringArrayHelper.parseFields(args[0],'.');
		if(parts.length>1 && parts[0].equals("system"))
		{
			triggers[loop] = (DataState)getEngine().getAPI().getSystemField(args[0]);
		}
		else
		{
			triggers[loop] = (DataState)getEngine().getAPI().getField(args[0]);
		}

		String[] ks = org.one.stone.soup.stringhelper.StringArrayHelper.parseFields(args[1],"+");
		for(int loop2=0;loop2<ks.length;loop2++)
		{
			ks[loop2] = ks[loop2].trim().toUpperCase();
			if(ks[loop2].length()==1)
			{
				keys[loop] = ks[loop2].charAt(0);
			}
			else if(ks[loop2].charAt(0)=='F')
			{
				keys[loop] = (char)Integer.parseInt(ks[loop2].substring(1));
				fn[loop] = true;
			}
			else if(ks[loop2].equals("DEL"))
			{
				keys[loop] = 127;
			}
			else if(ks[loop2].equals("SPACE"))
			{
				keys[loop]=' ';
			}
			else if(ks[loop2].equals("RETURN"))
			{
				keys[loop]=10;
			}
			else if(ks[loop2].equals("CTRL"))
			{
				ctrl[loop]=true;
			}
			else if(ks[loop2].equals("ALT"))
			{
				alt[loop]=true;
			}
			else
			{
				System.out.println("Key not recognised "+ks[loop2]);
			}
		}
		if(args.length==3)
		{
			if(args[2].toUpperCase().equals("TOGGLED"))
			{
				toggled[loop]=true;
			}
		}
	}
}
/**
 * process method comment.
 */
public void process()
{
 try{
	//System.out.println( "Key Code:"+ keyboard.keyDown((char)0x10) );
	for(int loop=0;loop<keys.length;loop++)
	{
		if(reset[loop]==true)
		{
			triggers[loop].setValue( false,this );
			reset[loop]=false;
		}
		if( fn[loop]==true )
		{
			if(DataKey.fnKeyDown(keys[loop])==true)
			{
				triggers[loop].setValue( true,this );
			}
			reset[loop]=true;
		}
		else if( DataKey.keyDown(keys[loop]) )
		{
			if(alt[loop]==true)
			{
				if(!DataKey.altDown())
					continue;
			}
			else
			{
				if(DataKey.altDown())
					continue;
			}
			if(ctrl[loop]==true)
			{
				if(!DataKey.ctrlDown())
					continue;
			}
			else
			{
				if(DataKey.ctrlDown())
					continue;
			}

			if(toggled[loop]==false)
			{
				triggers[loop].setValue( true,this );
				reset[loop]=true;
			}
			else
			{
				triggers[loop].setValue( !triggers[loop].getValue(),this );
			}
		}
	}
 }catch(LockException le)
 {}
}
/**
 * register method comment.
 */
public void register(org.one.stone.soup.mjdb.data.field.DataLinker store)
{
	map = grfxMLCaster.cast(map,getArg(map,0,store));
}
public void replace(Field oldObj, Field newObj)
{
	map = grfxMLCaster.replace(map,oldObj,newObj);
}
/**
 * stop method comment.
 */
public void stop() {}
}
